import React, { Component } from 'react';

import { HashRouter as Router, Route, Redirect, Switch, withRouter } from 'react-router-dom'
import { asyncComponent } from 'react-async-component';
import { inject, observer } from 'mobx-react';
import { Browser, DocumentTitle } from 'weaver-mobile-page'
import { Tools, SearchBar, Icon } from 'weaver-mobile';
import {WrAlertPage} from "../../public";

import PlanView from './planview'
import PlanResult from './planresult'
import PlanAudit from './planaudit'
import dict from '../../util/wea-workrelate-file';
dict.addWindowFunc();
const getLabel = Tools.getLabel;
@withRouter
@observer
class PlanIndex extends Component {
  constructor(props) {
    super(props)
    window.refreshPlanAuditNum = () => {
      this.updateNum();
    }
    this.state = {
      menuData: [ //菜单数据
        { path: '/planHome/planResult', menuname: getLabel('505885', '报告查询') },
        { path: '/planHome/planAudit', menuname: getLabel('505884', '报告审批') },
        { path: '/planHome/planView/index?myReport', menuname: getLabel('6015', '我的报告') },
      ],
      isRight:''
    }
  }

  componentWillMount() {
    if (window.refreshPlanAuditNum && typeof window.refreshPlanAuditNum === "function") {
      window.refreshPlanAuditNum();
    }
    this.checkRight()
  }

  render() {
    let { menuData,isRight } = this.state
    const data = [
      { match: /^\/planHome\/planView/i, title: () => getLabel('6015', '我的报告') },
      { match: /^\/planHome\/planAudit/i, title: () => getLabel('505884', '报告审批') },
      { match: /^\/planHome\/planResult/i, title: () => getLabel('505885', '报告查询') },
    ];
    if (isRight == "") {
        return <div></div>;
    }
    if (isRight == "0") {
        return <WrAlertPage show={true} msg={getLabel(509281,"您的系统未开启报告模块")}/>
    }
    return (
      
      <div>
        <DocumentTitle data={data} defaultTitle="" />
        <Switch>
          <Route id="planView"
            breadcrumbName={getLabel('6015', '我的报告')}
            path="/planHome/planView/index"
            render={() => <PlanView menuData={menuData} />} />
          <Route id="planView"
            breadcrumbName={getLabel('6015', '我的报告')}
            path="/planHome/planView"
            render={() => <PlanView menuData={menuData} />} />
          <Route id="planResult"
            breadcrumbName={getLabel('505885', '报告查询')}
            path="/planHome/planResult"
            render={() => <PlanResult menuData={menuData} />} />
          <Route id="planAudit"
            breadcrumbName={getLabel('505884', '报告审批')}
            path="/planHome/planAudit"
            render={() => <PlanAudit menuData={menuData} />} />
            <Redirect exact path="/planHome" to={{pathname: "/planHome/planResult"}}/>
        </Switch>
      </div>
    )
  }
  updateNum = () => {
    let options = {
      method: 'POST',
      url: '/api/workrelate/plan/noAuditCount',
      params: { operation: "all" }
    };
    Tools.callApi(options).then((data) => {
      if (data.api_status) {
        let { menuData } = this.state
        menuData[1].rightContent = data.noAuditCount
        this.setState({ menuData })
      }
    });
  }
  checkRight = ()=>{
    let options = {
      method: 'get',
      url: '/api/workrelate/common/checkHasRigth',
      params: { id: "4" }
    };
    Tools.callApi(options).then((data) => {
      if (data.status && data.isRight) {
        this.setState({isRight: '1'})
        this.updateNum();
    } else {
        this.setState({isRight: "0"});
    }
    });

  }
}

export default PlanIndex;